<script setup lang='ts'>
  import { onMounted, reactive } from 'vue';
  import { guild_roomApi } from '@/api/controller';
  import { ToastWarning, Toastloading } from '@/utils/public';
  import { getDateArray, getPlayEndTime } from '@/utils'
  import Pagination from "@/components/Pagination/index.vue";
  import table2excel from 'js-table2excel';

  const action = reactive({
    total:0,
    list:[] as any[],
    dateArr:getDateArray('ymdhis') as Array<any>
  })
  const listQuery = reactive<any>({
    page:1,
    limit:20,
    room_id:'',
    union_id: '',
    show_type:'',
  })
  const listStatus = [
    '已结算数据','本周即时统计','上周即时统计',
  ]
  const handleDownload = async()=>{
    const loading = Toastloading('数据导出中...')
    
    const query = { ...listQuery }
    query.page = 1
    query.limit = action.total || 20
    const timeObi = getPlayEndTime(action.dateArr)
    const res = await guild_roomApi.unionRoomWeekRewardList({
      ...query,
      created_st:timeObi.playTime,
      created_ed:timeObi.endTime
    })
    loading.close()
    const { data=[]} = res.data
    loading.close()
    if(data.length == 0) {
      ToastWarning('暂无可导出数据')
      return
    }
    const tHeader = [
      { title: '序号', key: "id"},
      { title: '公会id', key: "union_id"},
      { title: '公会名称', key: "union_name"},
      { title: '房间uid', key: "room_uid"},
      { title: '房间名称', key: "room_name"},
      { title: '公会房间总收入', key: "roomTotal"},
      { title: '阶梯比例', key: "stepRate"},
      { title: '阶梯奖励', key: "stepTotal"},
      { title: '额外比例', key: "extraRate"},
      { title: '额外奖励', key: "extraTotal"},
      { title: '会长邀请用户在公会房间的消费', key: "inviteTotal"},
      { title: '邀请奖励', key: "inviteRewardTotal"},
      { title: '结算时间', key: "created_at"}
    ]
    table2excel(tHeader, data, '公会周结算')
  }
  const handleSerach = ()=>{
    listQuery.page = 1
    if(listQuery.show_type != 0) {
      action.dateArr = []
    }
    getList()
  }
  const resetQuery = ()=>{
    Object.assign(listQuery,{
      page:1,
      limit:20,
      room_id:'',
      union_id: '',
      show_type:'',
    })
    action.dateArr = getDateArray('ymdhis') as Array<any>
    getList()
  }
  const getList = async()=> {
    const loading = Toastloading()
    const timeObi = getPlayEndTime(action.dateArr)
    const res = await guild_roomApi.unionRoomWeekRewardList({
      ...listQuery,
      created_st:timeObi.playTime,
      created_ed:timeObi.endTime
    })
    loading.close()
    const { data=[], total=0 } = res.data
    action.total = total
    action.list = data
  }
  onMounted(()=>{
    getList()
  })
</script>

<template>
  <div class="xm-content">
    <div class="xm-headTab">
      <div class="labelBox">
        <div class="labelItem">
          <div class="label">公会ID：</div>
          <el-input
            v-model="listQuery.union_id"
            placeholder="请输入公会ID"
          />
        </div>
        <div class="labelItem">
          <div class="label">房间ID：</div>
          <el-input
            v-model="listQuery.room_id"
            placeholder="请输入房间ID"
          />
        </div>
        <div class="labelItem">
          <div class="label">结算类型：</div>
          <el-select clearable v-model="listQuery.show_type">
            <el-option v-for="(item,index) in listStatus" :key="index" :label="item" :value="index" />
          </el-select>
        </div>
        <div class="labelItem">
          <div class="label">日期：</div>
          <el-date-picker
            v-model="action.dateArr"
            type="datetimerange"
            format="YYYY-MM-DD HH:mm:ss"
            value-format="YYYY-MM-DD HH:mm:ss"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"/>
        </div>
        <div class="labelItem">
          <el-button type="primary" @click="handleSerach">查询</el-button>
          <el-button type="primary" @click="resetQuery">重置</el-button>
          <el-button type="warning" @click="handleDownload">导出</el-button>
        </div>
      </div>
    </div>
    <el-table
      class="xm-main xm-TableCenter"
      :data="action.list"
      border>
      <el-table-column width="100" prop="id" label="序号" />
      <el-table-column width="100" prop="union_id" label="公会id" />
      <el-table-column width="180" show-overflow-tooltip prop="union_name" label="公会名称" />
      <el-table-column width="100" prop="room_uid" label="房间uid" />
      <el-table-column width="180" show-overflow-tooltip prop="room_name" label="房间名称" />
      <el-table-column width="140" prop="roomTotal" label="公会房间总收入" />
      <el-table-column width="140" prop="stepRate" label="阶梯比例" />
      <el-table-column width="140" prop="stepTotal" label="阶梯奖励" />
      <el-table-column width="140" prop="extraRate" label="额外比例" />
      <el-table-column width="140" prop="extraTotal" label="额外奖励" />
      <el-table-column width="140" prop="inviteTotal" label="会长邀请用户在公会房间的消费" />
      <el-table-column width="140" prop="inviteRewardTotal" label="邀请奖励" />
      <el-table-column min-width="210" prop="created_at" label="结算时间" />
    </el-table>
    <Pagination v-show="action.total > 0" :total="action.total" v-model:page="listQuery.page"
      v-model:limit="listQuery.limit" @pagination="getList" />
  </div>
</template>